// 设置 token 和过期时间
export const setToken = (token: string) => {
  const expireTime = new Date().getTime() + 3 * 24 * 60 * 60 * 1000  // 3天后过期
  localStorage.setItem('token', token)
  localStorage.setItem('tokenExpireTime', expireTime.toString())
}

// 检查 token 是否过期
export const isTokenExpired = () => {
  const token = localStorage.getItem('token')
  const expireTime = localStorage.getItem('tokenExpireTime')
  
  // 如果有 token 但没有过期时间，说明是旧数据，重新设置过期时间
  if (token && !expireTime) {
    const newExpireTime = new Date().getTime() + 3 * 24 * 60 * 60 * 1000
    localStorage.setItem('tokenExpireTime', newExpireTime.toString())
    return false
  }
  
  // 如果没有 token，不需要检查过期
  if (!token) return false
  
  // 检查是否过期
  return expireTime ? new Date().getTime() > parseInt(expireTime) : true
}

// 清除 token
export const removeToken = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('tokenExpireTime')
} 